package com.gzx.plugin.tjzy.modular.gzxcase.mapper;

import com.gzx.plugin.tjzy.modular.gzxcase.dto.GzxAllocateCaseDto;
import com.gzx.plugin.tjzy.modular.gzxcase.entity.CaseQueryParam;
import com.gzx.plugin.tjzy.modular.gzxcase.entity.GzxCase;
import com.gzx.plugin.tjzy.modular.gzxcase.vo.GzxCaseVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import vip.xiaonuo.common.mapper.BaseMapperPlus;

import java.util.List;

/**
 * 案件列Mapper接口
 *
 * @author LionLi
 * @date 2024-09-12
 */
public interface GzxCaseMapper extends BaseMapperPlus<GzxCase, GzxCaseVo> {

    /**
     * 批量修改调解员
     *
     * @param mediatorId
     * @param batchNumber
     * @param debtorCardIdList
     */
    @Update("<script>" +
            "update gzx_case set case_status = 2,affiliated_company = #{orgId},mediator_id = #{mediatorId},mediator_name = #{mediatorName} " +
            "where debtor_card_id in " +
            "<foreach item=\"item\" collection=\"debtorCardIdList\" open=\"(\" separator=\",\" close=\")\">" +
            " #{item}" +
            "</foreach>" +
            " and  batch_number = #{batchNumber}" +
            "</script>")
    void updateMediatorName(@Param("orgId") Long orgId, @Param("mediatorId") Long mediatorId, @Param("mediatorName") String mediatorName, @Param("batchNumber") String batchNumber, @Param("debtorCardIdList") List<String> debtorCardIdList);

    /**
     * 批量修改调解员
     *
     * @param allocateCaseDto
     */
    @Update("<script>" +
            "update gzx_case set case_status = 2,affiliated_company = #{dto.orgId},mediator_id = #{dto.mediatorId},mediator_name = #{dto.mediatorName} " +
            "where debtor_card_id in " +
            "<foreach item=\"item\" collection=\"dto.debtorCardIdList\" open=\"(\" separator=\",\" close=\")\">" +
            " #{item}" +
            "</foreach>" +
            " and  batch_number = #{dto.batchNumber}" +
            "</script>")
    void allocateCase(@Param("dto") GzxAllocateCaseDto allocateCaseDto);

    @Insert("<script>" +
            "INSERT INTO gzx_case (company_code, product_code, project_code, batch_number, case_status, " +
            "debtor_name, debtor_sex, debtor_phone, debtor_card_id, loan_amount, overdue_amount, " +
            "clean_loan_amount, tenant_id, create_time, create_user) " +
            "VALUES " +
            "<foreach collection='caseList' item='item' separator=','>" +
            "(#{item.companyCode}, #{item.productCode}, #{item.projectCode}, #{item.batchNumber}, " +
            "#{item.caseStatus}, #{item.debtorName}, #{item.debtorSex}, #{item.debtorPhone}, " +
            "#{item.debtorCardId}, #{item.loanAmount}, #{item.overdueAmount}, #{item.cleanLoanAmount}, " +
            "#{item.tenantId}, #{item.createTime}, #{item.createUser})" +
            "</foreach>" +
            "</script>")
    void insertBatchForExcelOld(@Param("caseList") List<GzxCase> caseList);

    /**
     * 批量保存案件
     *
     * @param valueSql
     */
    @Insert("<script>" +
            "INSERT INTO gzx_case (company_code,product_code,project_code,batch_number,case_status,debtor_name," +
            "debtor_sex,debtor_phone,debtor_card_id,intent_tag,penalty_interest,loan_amount,overdue_amount," +
            "clean_loan_amount,overdue_interest,overdue_fees,overdue_status,mediator_id,mediator_name," +
            "affiliated_company,partner_business_id,tenant_id,create_time) VALUES ${valueSql}" +
            "</script>")
    void insertBatch(String valueSql);

    /**
     * 查询案件ID和甲方系统主键
     *
     * @param params
     * @return
     */
    @Select({
            "<script>",
            "SELECT id, partner_business_id,debtor_card_id FROM gzx_case",
            "WHERE",
            "<foreach collection='params' item='param' open='(' separator=' OR ' close=')'>",
            "(batch_number = #{param.batchNumber} AND debtor_card_id IN",
            "<foreach collection='param.debtorCardIdList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>)",
            "</foreach>",
            "</script>"
    })
    List<GzxCase> listCasesByBatchAndDebtorIds(@Param("params") List<CaseQueryParam> params);
}
